<template>
  <div>
    <el-dialog v-model="open" :show-close="false" width="700px" @open="initOpen" @close="close">
      <template #title>
        <svg-icon icon-class="list"></svg-icon>
        <span style="padding: 0 10px">成果图审核</span>
        <div style="float:right">
          <el-button type="primary" @click="changeType('0')" v-if="!examineType">通过</el-button>
          <el-button type="primary" @click="changeType('1')">审核不通过</el-button>
        </div>
      </template>
      <template #default>
        <el-row :gutter="20" v-loading="loading">
          <el-col :span="14" v-if="!isBatch">
            <ImagePreview :src="`/attachment/downLoadFile?attachId=${data.attachId}`" />
          </el-col>
          <el-col :span="isBatch ? 24 : 10">
            <el-form :model="form" ref="formRef" :label-position="type === '0' ? 'right' : 'top'">
              <template v-if="type === '0'">
                <el-radio-group v-model="radioEmail" style="display: block" @change="radioEmailChange">
                  <p class="radio-warp">
                    <el-radio label="1">
                      <el-form-item label="邮箱地址" prop="email">
                        <el-select v-model="form.email" :disabled="radioEmail == '2'">
                          <el-option v-for="item in emailOption" :key="item.id" :label="item.email"
                            :value="item.email"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-radio>
                  </p>
                  <el-row class="radio-warp">
                    <el-radio label="2">
                      <el-form-item label="手动输入" prop="handEmail">
                        <el-input v-model="form.handEmail" :disabled="radioEmail == '1'"></el-input>
                      </el-form-item>
                    </el-radio>
                  </el-row>
                </el-radio-group>
                <el-form-item>
                  <el-button type="primary" @click="push">推送</el-button>
                </el-form-item>
              </template>
              <template v-else-if="type === '1'">
                <el-form-item prop="reason" label="审核不通过原因" :rules="[{ required: true, message: '请填写审核不通过原因' }]">
                  <el-input type="textarea" v-model="form.reason" :autosize="{ minRows: 4, maxRows: 10 }"
                    resize="none"></el-input>
                </el-form-item>
                <el-form-item v-if="!examineType">
                  <el-button type="primary" @click="confirm">确认</el-button>
                </el-form-item>
              </template>
            </el-form>
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import ImagePreview from '@/components/ImagePreview'
import { getEmail, examine } from '@/api/quickMapping/achievementReview'

const props = defineProps({
  open: Boolean,
  data: Object,
  examineType: String
})
const emits = defineEmits(['update:open', 'examineSuccess', 'update:examineType'])
const { proxy } = getCurrentInstance()

const isBatch = computed(() => Array.isArray(props.data))

const form = ref({})
const type = ref('')
const changeType = value => type.value = value
const radioEmail = ref('1')

const ids = ref([])
const initOpen = () => {
  getEmailList()
  type.value = isBatch.value ? '0' : ''
  ids.value = isBatch.value ? props.data : [props.data.id]
  // 审核不通过-查看
  if (props.examineType === '1') {
    type.value = '1'
    const { checkReason } = props.data
    form.value.reason = checkReason
  }

}
const radioEmailChange = () => {
  form.value.email = ''
  form.value.handEmail = ''
}
// 邮箱
const emailOption = ref([])
const getEmailList = () => {
  getEmail().then(res => {
    emailOption.value = res.data
  })
}
// 推送
const push = () => {
  const { email, handEmail } = form.value
  let emailVal = radioEmail.value == '1' ? email : handEmail
  pass({ ids: ids.value, email: emailVal })
}
// 确认
const confirm = () => {
  proxy.$refs.formRef.validate(valid => {
    if (valid) {
      pass({ ids: ids.value, reason: form.value.reason })
    }
  })
}
const loading = ref(false)
const pass = (data) => {
  loading.value = true
  examine(data).then(res => {
    if (res.code === 200) {
      loading.value = false
      proxy.$modal.msgSuccess('审核完成')
      emits('update:open', false)
      emits('examineSuccess')
    }else{
      loading.value = false
    }
  }).catch(()=>{
    loading.value = false
  })
}
const close = () => {
  proxy.resetForm('formRef')
  ids.value = []
  emits('update:open', false)
  emits('update:examineType', '')
}
</script>
<style lang="scss" scoped>
.radio-warp {
  padding: 10px 0;

  .el-form-item.el-form-item--default {
    margin-bottom: 0;
  }
}
</style>